/*
 *  Copyright (C) 2011 yudi wibisono (yudi1975@gmail.com)
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.upi.cs;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 *
 * @author yudi wibisono
 */
public class Stat {

    public class Item {
          String itemNo;
          int numOrder=0;
          int numBasket=0;
          int numView=0;
    }


      public void hitungFreqTrans(String inputFile) {
         //menghitung freq item order/basket/view
         //diurutkan berdasaran freq order/freq basket/freq view
         //misal: 1234|2323|3004|5000 --> item1234, order 2323, basket 3004, view 5000

      HashMap<String,Item> hmItem = new  HashMap<String,Item>();
      
      Logger log = Logger.getLogger("edu.cs.upi.tweetmining");
      String inputFileWoExt = inputFile.substring(0, inputFile.lastIndexOf('.')); //without ext
      String namaFileOutput = inputFileWoExt+"_freqItem.txt";
      try {
            PrintWriter pw = new PrintWriter(namaFileOutput);
            FileInputStream fstream = new FileInputStream(inputFile);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int cc=0;
            Item itm;
            while ((strLine = br.readLine()) != null)   {
                cc++;
                System.out.println(cc);
                String[] temp;
                temp = strLine.split("\\|");

                itm = hmItem.get(temp[1]);
                if (itm==null) {
                    itm = new Item();
                    hmItem.put(temp[1], itm);
                    itm.itemNo = temp[1];
                }

                //bukan else, karena kalau pake else maka tidak diproses
                if (temp[2].equals("0")) {
                     itm.numView++;
                } else
                if (temp[2].equals("1")) {
                     itm.numBasket++;
                } else {
                     itm.numOrder++;
                }
                

                //testing
                //if ( (cc % 1000) ==0) {
                    //pw.flush();
                //    break;  //testing dulu
                //}
            }

            //save statistiknya
            for (Map.Entry<String,Item> entry : hmItem.entrySet())
            {
                Item it=entry.getValue();
                pw.println(entry.getKey() + "|" + it.numView + "|" + it.numBasket + "|" + it.numOrder);
            }

            pw.close();
            br.close();
            in.close();
            fstream.close();
            } catch (Exception e) {
                log.severe(e.toString());
       }
     }

     public static void main(String[] args) {
         Stat s = new Stat();
         s.hitungFreqTrans("F:\\dataminingcup\\task1\\transact_train_sessionwithtransOrder.txt");
         //s.hitungFreqTrans("F:\\dataminingcup\\task1\\transact_train.txt");
         //s.hitungFreqTrans("F:\\dataminingcup\\task1\\transact_test.txt");
     }
}
